package com.sinodata.bsm.cicp.finder.linux;

/**
 * 文件系统发现程序.详情查看"linux监控功能说明.xls"
 * @author zhaoch
 * @version 1.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class LinuxDisk extends LinuxFinder {

    private static final Logger logger = Logger.getLogger(LinuxDisk.class);
    private static final String CMDSTR = "iostat -d|sed -e '1,3d'|awk '{print $1}'";

    private String getDiskType(String diskName) {
        if (diskName.indexOf("hd") != -1) {
            return "IDE";
        } else if (diskName.indexOf("sd") != -1) {
            return "SCSI";
        } else {
            return "UNKNOWN";
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] diskInfo = null;
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            String strLine = null;
            boolean flag = false;
            while ((strLine = reader.readLine()) != null) {
                if (strLine.startsWith("scd0")) {
                    flag = true;
                } else if (flag) {
                    diskInfo = new String[5];
                    diskInfo[0] = strLine;
                    diskInfo[1] = "Disk" + strLine;
                    diskInfo[2] = super.getHostName(ip) + " Disk " + strLine;
                    list.add(diskInfo);
                    String[] ss = strLine.split("\\s+");
                    diskInfo[3] = ss[0];//硬盘标识
                    diskInfo[4] = getDiskType(ss[0]);//硬盘类型
                }
            }
        } catch (IOException e) {
            logger.error("failed to find linux disk", e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                logger.error("failed to close reader", e);
            }
        }
        return list;
    }
}
